import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CodeCount {

	static int normallines = 0;
	static int commentlines = 0;
	static int whitelines = 0;

	public static void main(String[] args) {

		File f = new File("D:\\JavaProject\\TankWar3.0\\src\\com\\hongxf\\tank");
		File[] codeFiles = f.listFiles();
		for (File child : codeFiles) {
			if (child.getName().matches(".*\\.java$")) {
				parse(child);
			}
		}

		System.out.println("normallines: " + normallines);
		System.out.println("whitelines: " + whitelines);
		System.out.println("commentlines: " + commentlines);
	}

	private static void parse(File f) {

		BufferedReader br = null;
		String line = "";
		boolean comment = false;

		try {
			br = new BufferedReader(new FileReader(f));
			while ((line = br.readLine()) != null) {
				line = line.trim();
				if (line.matches("^[\\s&&[^\\n]]*")) {
					whitelines++;
				} else if (line.startsWith("/*") && !line.endsWith("*/")) {
					commentlines++;
					comment = true;
				} else if (comment == true) {
					commentlines++;
					if (line.endsWith("*/")) {
						comment = false;
					}
				} else if (line.startsWith("//")) {
					commentlines++;
				} else if (line.startsWith("/*") && line.endsWith("*/")) {
					commentlines++;
				} else {
					normallines++;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != br) {
				try {
					br.close();
					br = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
